import numpy as np

# a = np.arange(4).reshape(-1, 2)
# print(a)
#
# b = np.arange(-4, 0).reshape(-1, 2)
# print(b)
#
# print(a @ b)

# 利用列表推导式写矩阵乘法
# M1 = np.random.rand(2, 3)
M1 = np.arange(4).reshape(-1, 2)
# M2 = np.random.rand(3, 4)
M2 = np.arange(-4, 0).reshape(-1, 2)
res = np.empty((M1.shape[0], M2.shape[1]))

print("M1:\n", M1)
print("M2:\n", M2)

for i in range(M1.shape[0]):
    for j in range(M2.shape[1]):
        item = 0
        print("*" * 10, f"res第{i + 1}行 第{j + 1}列", "*" * 10)
        for k in range(M1.shape[1]):
            print(
                f"矩阵M1：第{i + 1}行第{k + 1}列",
                f"矩阵M2：第{k + 1}行第{j + 1}列",
            )
            item += M1[i][k] * M2[k][j]
        print("\n")
        res[i][j] = item

print("res:\n", res)

# 列表推导式
res = [[sum([M1[i][k] * M2[k][j] for k in range(M1.shape[1])]) for j in range(M2.shape[1])] for i in range(M1.shape[0])]
